﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports CodeMatrix.Context

' Created by Boonyarit Yingvoragan
' Created date 30/10/2553
Public Class StpAuthenGroup

    Public Shared Function Delete(ByVal tbl As String, ByVal pid As String, ByVal cid As String) As Int32

        Dim affectedRecord = -1
        Using c As New SqlConnection(CodeMatrix.Utilities.CodeHelper.ConnectionString)

            Dim cmd As New SqlCommand()
            cmd.CommandText = "EXEC  [dbo].[USP_GenericDelete] @tbl,@pid,@cid;"
            cmd.Connection = c

            cmd.Parameters.Add(New SqlParameter("@tbl", tbl))
            cmd.Parameters.Add(New SqlParameter("@pid", pid))
            cmd.Parameters.Add(New SqlParameter("@cid", cid))

            If c.State <> ConnectionState.Open Then
                c.Open()
            End If
            affectedRecord = cmd.ExecuteNonQuery()

            ' =========================================================
            ' Log command
            ' =========================================================
            Dim e As New OrderedDictionary()
            e.Add(pid, cid)
            GenericHelper.GenerateScript(GenerateScriptLogType.Delete, LogMenuType.stp_auth_group, e)
        End Using

        Return affectedRecord

    End Function

    Public Shared Function GetData(ByVal department_code As Int32, ByVal group_code As Int32, ByVal auth_group_status As Int32, ByVal staff_code As Int32) As DataTable

        ' =========================================================================
        ' Get table
        ' =========================================================================
        Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            "exec USP_GetListAuthenGroup @staff_code, @department_code, @group_code , @auth_group_status", _
             New SqlParameter("@staff_code", staff_code), _
             New SqlParameter("@department_code", department_code), _
             New SqlParameter("@group_code", group_code), _
             New SqlParameter("@auth_group_status", auth_group_status) _
            )

        'tbl = StpDepartment.ArrangeData(tbl)

        Return tbl

    End Function

    Public Shared Function GetDataForReport(ByVal staff_code As Int32, ByVal department_code As Int32, ByVal group_code As Int32, ByVal auth_group_status As Int32, ByVal main_system_code As Int32, ByVal menu_system_code As Int32) As DataTable

        ' =========================================================================
        ' Get table
        ' =========================================================================
        Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            "exec USP_List_stp_auth_group1 @staff_code, @group_code , @department_code, @main_system_code, @menu_system_code,@auth_group_status", _
            New SqlParameter("@staff_code", staff_code), _
            New SqlParameter("@group_code", group_code), _
            New SqlParameter("@department_code", department_code), _
            New SqlParameter("@main_system_code", main_system_code), _
            New SqlParameter("@menu_system_code", menu_system_code), _
            New SqlParameter("@auth_group_status", auth_group_status) _
            )

        tbl = StpDepartment.GetEmptyColumn(tbl)

        Return tbl

    End Function

    Public Shared Function GetDepartmentForAuthGroup(ByVal staff_code As Int32, ByVal department_code As Int32, ByVal group_code As Int32, ByVal auth_group_status As Int32, ByVal main_system_code As Int32, ByVal menu_system_code As Int32) As DataTable

        ' =========================================================================
        ' Get table
        ' =========================================================================
        Dim tbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
            "exec USP_List_stp_auth_group1 @staff_code, @group_code , @department_code, @main_system_code, @menu_system_code,@auth_group_status", _
            New SqlParameter("@staff_code", staff_code), _
            New SqlParameter("@group_code", group_code), _
            New SqlParameter("@department_code", department_code), _
            New SqlParameter("@main_system_code", main_system_code), _
            New SqlParameter("@menu_system_code", menu_system_code), _
            New SqlParameter("@auth_group_status", auth_group_status) _
            )

        ' Modified by Boonyarit Yingvoragan
        ' Modified date 19/10/2553
        tbl = GetDepartment(tbl)

        Return tbl

    End Function

    Public Shared Function GetDepartment(ByVal srcTbl As DataTable) As DataTable

        Dim result As String = ""

        For i As Integer = 0 To srcTbl.Rows.Count - 1

            result = GetParents(srcTbl.Rows(i)("group_code"))

            srcTbl.Rows(i)("dept_hirachy") = result

            'srcTbl.Rows(i)("department_hirachy") = result & "," & srcTbl.Rows(i)("department_hirachy")

        Next

        Return srcTbl

    End Function

    Private Shared Function GetParents(ByVal groupCode As Integer) As String

        Dim output As String = ""
        Dim dep_parent As Integer = 0

        ' Get department id
        Dim reTbl = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
                   "select department_code from group_user where group_user_code = " & groupCode)

        Dim departmentcode As Integer = reTbl.Rows(0)(0)

        ' Call first time
        Dim reTblParent = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
                    "select department_parent from stp_department where department_code = " & departmentcode)

        Do While Not (reTblParent.Rows(0)(0) Is DBNull.Value)

            Dim reTblDepartName = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
                    "select department_name from stp_department where department_code = " & reTblParent.Rows(0)(0))

            If (Not String.IsNullOrEmpty(output)) Then
                'output = output & ","
                output = "," & output
            End If


            'output = output & reTblParent.Rows(0)(0).ToString()
            output = reTblDepartName.Rows(0)(0).ToString() & output

            ' Get next one
            reTblParent = CodeMatrix.Utilities.CodeHelper.GetDataTable( _
                    "select department_parent from stp_department where department_code = " & reTblParent.Rows(0)(0).ToString())
        Loop

        Return output

    End Function

End Class
